package android.longs.study.user.service;

import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;

import com.sun.org.apache.regexp.internal.recompile;

import android.longs.study.base.ResponeModel;
import android.longs.study.user.dao.clas.ClassModelDao;
import android.longs.study.user.dao.clas.ClassModelDaoImple;
import android.longs.study.user.model.clas.JavaClassModel;

public class ClassService {

    private ClassModelDao modelDao = new ClassModelDaoImple();
    public void addClass(JavaClassModel model) {
	if (model!=null) {
	    modelDao.addClass(model);
	}
    }

    public void updateClass(JavaClassModel model) {
	if (model!=null) {
	    modelDao.updateClass(model);
	}
    }

    public JavaClassModel queryFromId(String id) {
	if (id!=null&&!id.equals("")) {
	    JavaClassModel queryFormID = modelDao.queryFormID(id);
	    return queryFormID;
	} else {
	    return null;
	}
    }

    public JavaClassModel queryFromName(String name) {
	if (name!=null&&!name.equals("")) {
	    JavaClassModel queryFormID = modelDao.queryFormClassName(name);
	    return queryFormID;
	} else {
	    return null;
	}
    }
    public String queryAll(){
	
	ResponeModel<List<JavaClassModel>> responeModel = new ResponeModel<>();
	
	
	try {
	    List<JavaClassModel> list = modelDao.queryAllClass();
	    responeModel.content = list;
	    responeModel.message="请求成功";
	    responeModel.code="1000";
	} catch (Exception e) {
	    responeModel.message="请求异常";
	    responeModel.code="1001";
	}
	
	JSON json = JSONSerializer.toJSON(responeModel);
	
	return json.toString();
    }

    public List<JavaClassModel> queryAll(int start,int end){
	if (start<0) {
	    start = 0;
	}
	if (end<0) {
	    end = 5;
	}
	return modelDao.queryAllClass(start,end);
    }
}
